﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CommandProcessor.Dispatcher;
using Data.Models;
using Data.Repositories;
using Microsoft.Reporting.WebForms;
using QuanLySanXuat.Models;

namespace QuanLySanXuat.Controllers
{
    public class PriceMaterialController : Controller
    {
        private readonly IOrderProcessRepository orderProcessRepository;
        private readonly IOrderRepository orderRepository;
        private readonly ICostDetailRepository costDetailRepository;
        private readonly IDesignDetailRepository designRepository;
        private readonly IPriceMaterialtRepository priceMaterialRepository;
        private readonly ICommandBus commandBus;
        public PriceMaterialController(ICommandBus commandBus, IOrderProcessRepository orderProcessRepository, IOrderRepository orderRepository, ICostDetailRepository costDetailRepository, IDesignDetailRepository designRepository, IPriceMaterialtRepository priceMaterialRepository)
        {
            this.commandBus = commandBus;
            this.orderProcessRepository = orderProcessRepository;
            this.orderRepository = orderRepository;
            this.costDetailRepository = costDetailRepository;
            this.designRepository = designRepository;
            this.priceMaterialRepository = priceMaterialRepository;
        }
        //
        // GET: /PriceMaterial/

        public ActionResult Index()
        {
            //Chọn nhung lenh san xuat da duoc thuc hien san xuat
            ViewData["Order"] = orderRepository.GetMany(x=>x.Status!=0);
            return View();
        }
        public ActionResult PrintView(int ID)
        {
            int value = 0;
            var oderpc=orderProcessRepository.Get(x => x.OrderID == ID);
            if (oderpc != null)
            {
                value = oderpc.ID;
            }
            var parameter = new SqlParameter
            {
                DbType = DbType.Int32,
                ParameterName = "@oderProcessID",
                Value = value
            };

            var priceMaterial = priceMaterialRepository.ExecWithStoreProcedure
                                         ("PriceMaterial @oderProcessID ",
                                            parameter
                                         ).ToList();

            return PartialView("_PriceMaterialList", priceMaterial);
        }
        public FileContentResult Print(int ID)
        {

            LocalReport localReport = new LocalReport();
            localReport.ReportPath = Server.MapPath("~/Report/Reports/RptPriceMaterial.rdlc");

            var oderprocess = orderProcessRepository.Get(x => x.OrderID == ID);
            ReportParameter p1 = new ReportParameter("NameProduct", oderprocess.Code);


            var parameter = new SqlParameter
            {
                DbType = DbType.Int32,
                ParameterName = "@oderProcessID",
                Value = orderProcessRepository.Get(x => x.OrderID == ID).ID
            };

            var priceMaterial = priceMaterialRepository.ExecWithStoreProcedure
                                         ("PriceMaterial @oderProcessID ",
                                            parameter
                                         ).ToList();
            localReport.DataSources.Clear();
            ReportDataSource reportDataSource = new ReportDataSource("DsPriceMaterial", priceMaterial);
            localReport.SetParameters(new ReportParameter[] { p1 });
            localReport.DataSources.Add(reportDataSource);
            localReport.Refresh();


            string reportType = "PDF";
            string mimeType;
            string encoding;
            string fileNameExtension;
            //The DeviceInfo settings should be changed based on the reportType            
            //http://msdn2.microsoft.com/en-us/library/ms155397.aspx            
            string deviceInfo = "<DeviceInfo>" +
                "  <OutputFormat>jpeg</OutputFormat>" +
                "  <PageWidth>8.5in</PageWidth>" +
                "  <PageHeight>11in</PageHeight>" +
                "  <MarginTop>0.5in</MarginTop>" +
                "  <MarginLeft>1in</MarginLeft>" +
                "  <MarginRight>1in</MarginRight>" +
                "  <MarginBottom>0.5in</MarginBottom>" +
                "</DeviceInfo>";
            Warning[] warnings;
            string[] streams;
            byte[] renderedBytes;
            //Render the report            
            renderedBytes = localReport.Render(reportType, deviceInfo, out mimeType, out encoding, out fileNameExtension, out streams, out warnings);
            //Response.AddHeader("content-disposition", "attachment; filename=NorthWindCustomers." + fileNameExtension); 
            return File(renderedBytes, "pdf");
        }
    }
}
